<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script src="<c:url value="/resources/js/vendor/jquery.js" />"></script>
<script type="text/javascript">
	function autoSendAjaxRequest(type, value1, value2) {
		updateParameterFilter(type,value1,value2);
		showLoadingSearchResult();
		$("#lastPostsLoader").empty();
		if(response.length < flightLength && flightResult.length == 0){
			var index=response.length;
			ajaxRequest(index, 10, sortIndex, true);
		} else {
			loadFlightResult();
			unShowLoadingSearchResult();
		}
	}
	//========================================MAX STOPS SLIDER=========================================================//
	
	
	
	var flightResultTemp = new Array();
	
	var conditions = new Object();
	conditions.departRange = new Object();
	conditions.returnRange = new Object();
	conditions.durationRange = new Object();
	jQuery.noConflict();
	
	//function for load all slider
	function initSlider(){
		// don't display return slider when oneWay
		if (jsonParam.typeOfTrip == 'oneWay') {
			$("#divreturn").hide();
		}
		//Call the Slider
		$('#slider_numofstop').slider({
			//Config
			range : "min",
			min : 0,
			max : 2,
			create: function( event, ui ) {
				console.log("create slider stop");
			},
			slide : function(event, ui) {
				var valueSpan = ui.value;
				if (valueSpan == 0) {
					$('#lblNumofStop').text(valueSpan + " stop (direct)");
				} else if (valueSpan == 1) {
					$('#lblNumofStop').text(valueSpan + " stop");
				} else if (valueSpan == 2) {
					$('#lblNumofStop').text(valueSpan + " stops+");
				}
				$('#txtnumStop').val(valueSpan);
			},
		});
		
		
		var valueSpan = jsonParam.numStop;
		var valueParamFilter = jsonParamFilter.numStopFilter;
		console.log("paramFilter.numStopFilter: " + valueParamFilter);
		console.log("numStop: " + valueSpan);
		console.log("Init value for conditions");
		if(valueParamFilter == 2) {
			valueParamFilter = valueSpan;
		}
		if(valueSpan != valueParamFilter){
			if (valueParamFilter == 0) {
				$('#lblNumofStop').text(valueParamFilter + " stop (direct)");
			} else if (valueParamFilter == 1) {
				$('#lblNumofStop').text(valueParamFilter + " stop");
			} else if (valueParamFilter == 2) {
				$('#lblNumofStop').text(valueParamFilter +  " stops+");
			}
			$("#slider_numofstop").slider("option", "value", valueParamFilter);
			conditions.numStop = valueParamFilter;
			
		}else{
			if (valueSpan == 0) {
				$('#lblNumofStop').text(valueSpan + " stop (direct)");
			} else if (valueSpan == 1) {
				$('#lblNumofStop').text(valueSpan + " stop");
			} else if (valueSpan == 2) {
				$('#lblNumofStop').text(valueSpan +  " stops+");
			}
			$("#slider_numofstop").slider("option", "value", valueSpan);
			conditions.numStop = valueSpan;
		}
		conditions.departRange.start = jsonParamFilter.departMin;
		conditions.departRange.end = jsonParamFilter.departMax;
		conditions.returnRange.start = jsonParamFilter.returnMin;
		conditions.returnRange.end = jsonParamFilter.returnMax;
		conditions.durationRange.start = jsonParamFilter.durationMin
		conditions.durationRange.end = jsonParamFilter.durationMax;
		
		console.log(conditions.durationRange.end);
		console.log(conditions.durationRange.start);
		
		var departMin = jsonParamFilter.departMin;
		var departMax = jsonParamFilter.departMax;
		
		$("#slider_depart").slider({
			range : true,
			min : 0,
			max : 287,
			values : [ departMin, departMax ],
			create: function( event, ui ) {
				console.log("create slider depart");
			},
			slide : function(event, ui) {
				$("#lblDepartMin").text(convertToTime(ui.values[0]));
				$("#lblDepartMax").text(convertToTime(ui.values[1]));
				$('#departMin').val(ui.values[0]);
				$('#departMax').val(ui.values[1]);
			},
		});
		
		//display
		$("#lblDepartMin").text(convertToTime(departMin));
		$("#lblDepartMax").text(convertToTime(departMax));

		// silder return
		var returnMin = jsonParamFilter.returnMin;
		var returnMax = jsonParamFilter.returnMax;
		$("#slider_return").slider(
				{
					range : true,
					min : 0,
					max : 287,
					values : [ returnMin, returnMax ],
					slide : function(event, ui) {
						$("#lblReturnMin").text(convertToTime(ui.values[0]));
						$("#lblReturnMax").text(convertToTime(ui.values[1]));
						$('#returnMin').val(ui.values[0]);
						$('#returnMax').val(ui.values[1]);
					}
				});

		//display
		$("#lblReturnMin").text(convertToTime(returnMin));
		$("#lblReturnMax").text(convertToTime(returnMax));

		// slider duration
		var durationMin = jsonParamFilter.durationMin;
		var durationMax = jsonParamFilter.durationMax;
		$("#slider_duration").slider(
				{
					range : true,
					min : 0,
					max : 287,
					values : [ durationMin, durationMax ],
					slide : function(event, ui) {
						var durationEnd = ui.values[1];
						if (durationEnd >= 287) {
							$("#lblDurationMax").text("(Any)");
						} else {
							$("#lblDurationMax").text(convertToTime(ui.values[1]));
						}
						$("#lblDurationMin").text(convertToTime(ui.values[0]));
						$('#durationMin').val(ui.values[0]);
						$('#durationMax').val(ui.values[1]);
					}
				});
		
		//display
		if (durationMax >= 287){
			$("#lblDurationMax").text("(Any)");
		}
		else {
			$("#lblDurationMax").text(convertToTime(durationMax));
		}
		$("#lblDurationMin").text(convertToTime(durationMin));

	}

	function sliderFilter() {
						//===== handle event on filter
						// stop slider
						$("#slider_numofstop")
								.on(
										"slidestop",
										function(event, ui) {
											
											console
													.log("slider_numofstop: flightResult IN = "
															+ flightResult.length);
											console
													.log("slider_numofstop: response IN = "
															+ response.length);
											//handle number of stop filter
											conditions.numStop = ui.value;
											console
													.log("slider_numofstop before go into filter function : "
															+ conditions.numStop);
											
											flightResult = response
													.filter(function(e) {
														return compare(e);
													});
											console
													.log("slider_numofstop: flightResult OUT = "
															+ flightResult.length);
// 											$("#lastPostsLoader").empty();
// 											resetAmountOfAirline();
											autoSendAjaxRequest(1,ui.value,"");
										});
						//departure slider
						$("#slider_depart")
								.on(
										"slidestop",
										function(event, ui) {
// 											showLoading();
											//handle number of departure filter
											console
													.log("slider_depart: flightResult IN = "
															+ flightResult.length);
											conditions.departRange.start = ui.values[0];
											conditions.departRange.end = ui.values[1];
											console.log("slider_depart value: "
													+ ui.values[0] + ": "
													+ ui.values[1]);
											flightResult = response
													.filter(function(e) {
														return compare(e);
													});
											console
													.log("slider_depart: flightResult OUT = "
															+ flightResult.length);
// 											$("#lastPostsLoader").empty();											
// 											resetAmountOfAirline();
											autoSendAjaxRequest(2,ui.values[0],ui.values[1]);
											
										});
						//return slider
						$("#slider_return")
								.on(
										"slidestop",
										function(event, ui) {
// 											showLoading();
											//handle number of departure filter
											console
													.log("slider_return: flightResult IN = "
															+ flightResult.length);
											conditions.returnRange.start = ui.values[0];
											conditions.returnRange.end = ui.values[1];
											console.log("slider_return value: "
													+ ui.values[0] + ": "
													+ ui.values[1]);
											flightResult = response
													.filter(function(e) {
														return compare(e);
													});
											console
													.log("slider_return: flightResult OUT = "
															+ flightResult.length);
											
// 											resetAmountOfAirline();
											autoSendAjaxRequest(3,ui.values[0],ui.values[1]);
										});
						//duration slider
						$("#slider_duration")
								.on(
										"slidestop",
										function(event, ui) {
// 											showLoading();
											//handle number of departure filter
											console
													.log("slider_duration: flightResult IN = "
															+ flightResult.length);
											conditions.durationRange.start = ui.values[0];
											conditions.durationRange.end = ui.values[1];
											console
													.log("slider_duration value: "
															+ ui.values[0]
															+ ": "
															+ ui.values[1]);
											flightResult = response
													.filter(function(e) {
														return compare(e);
													});
											console
													.log("slider_duration: flightResult OUT = "
															+ flightResult.length);
// 											$("#lastPostsLoader").empty();
											autoSendAjaxRequest(4,ui.values[0],ui.values[1]);
										});
					}


	//method for filtering region
	function compare(e) {
		return (compareStop(e) && compareDepart(e) && compareReturn(e)
				&& compareDuration(e) && compareAirline(e));
		// && compareAirline(e)
	}
	function compareStop(e) {
		if (e.roundTrip == true) {
			if ((e.outBound.numberStops <= conditions.numStop)
					&& (e.inBound.numberStops <= conditions.numStop)){
					return true;
				}	
		} else {
			if (e.outBound.numberStops <= conditions.numStop)
				{
				return true;
				}
				
		}
		console.log("flight was removed: "+e.outBound.numberStops + "conditions: "+ conditions.numStop);
		console.log("false compareStop");

		return false;
	}
	function compareDepart(e) {
		// 		console.log("compare departure - departRange= "+conditions.departRange.start +": "+conditions.departRange.end);

		var time = e.outBound.departAirports[0].departureTimeByHHmm.split(':');
		var hour = time[0];
		var minute = time[1];
		var departureTime = parseInt(hour) * 12 + parseInt(minute) / 5;
		if (departureTime >= conditions.departRange.start
				&& departureTime <= conditions.departRange.end)
			return true;
		console.log("false compareDepart");
		return false;
	}
	function compareReturn(e) {
		if (e.roundTrip == true) {
			var time = e.inBound.departAirports[0].departureTimeByHHmm
					.split(':');

			var hour = time[0];
			var minute = time[1];
			var returnTime = parseInt(hour) * 12 + parseInt(minute) / 5;
			// 			console.log("return time after parsing: "+returnTime);
			if (returnTime >= conditions.returnRange.start
					&& returnTime <= conditions.returnRange.end) {
				return true;
			}

			console.log("false compareReturn");
			return false;
		}
		return true;
	}
	function compareDuration(e) {
		var durationOB = parseInt(e.outBound.totalDuration)/5;
		
		if (e.roundTrip == false) {
			if (conditions.durationRange.end == 287)
			{
				if (durationOB >= conditions.durationRange.start){
					return true;
				}
			}
			if (durationOB>= conditions.durationRange.start
					&& durationOB <= conditions.durationRange.end){
				return true;
			}
		}
		else
		{
			var durationIB = parseInt(e.inBound.totalDuration)/5;
			if (conditions.durationRange.end == 287)
			{
				if((durationIB >= conditions.durationRange.start) &&(durationOB >= conditions.durationRange.start)){
					return true;
				}
			}
			if ((durationOB >= conditions.durationRange.start) && (durationOB <= conditions.durationRange.end) && (durationIB >= conditions.durationRange.start) && (durationIB <= conditions.durationRange.end))
			{
						return true;
			}
		}
// 		console.log("durationOB: " + durationOB);
		console.log("false compare duration");
		return false;
	}
	//airline compare
	//oneway
	//1: list tac ca cac airline duoc checked, item.check=true;
	//2: so sanh carrier co nam trong list tren hay khong?
	//+ neu co: return true;
	//+ neu khong: return false;
	//voi truong hop roundTrip, vi chi can 1 trong 2 chuyen di va ve, co airline nam trong list thi co the return true.
	//neu outbound ma k co, thi check inbound, tuong tu nhu tren.
	function compareAirline(e) {
		if (e.roundTrip == false) {
			var carrier = e.outBound.departAirports[0].airline.name.replace(/[^a-zA-Z0-9]/g,' ');
			if (isExistAndCheckedInCarriers(carrier) == true){
				return true;
			}
			console.log("false compare airline");
			console.log("airline was removed: " + carrier);
			return false;
		} else {
			//the trip is round trip
			var carrierOB = e.outBound.departAirports[0].airline.name.replace(/[^a-zA-Z0-9]/g,' ');
			var carrierIB = e.inBound.departAirports[0].airline.name.replace(/[^a-zA-Z0-9]/g,' ');
			if (isExistAndCheckedInCarriers(carrierOB) == true
					&& isExistAndCheckedInCarriers(carrierIB) == true)
				return true;
			//den vi tri nay, ca 2 carrier da nam trong carriers, va check=false;return false;
			console.log("false compare airline");
			console.log("airline was removed: " + carrierOB + "- " + carrierIB);
			return false;
		}
		return true;
	}

	//check carrier if it exist and is checked in carriers
	function isExistAndCheckedInCarriers(carrier) {
		var cont = 0;
		$.each(jsonAirlines,function(index, carriers) {
			if (carriers.name == carrier) {
				if (carriers.check == true){
					cont++;
					return false;
				}
			}
		});
		if(cont > 0){
			return true;
		}
		else{
			return false;
		}
	}
	
// 		for (i = 0; i < carriers.length; i++)
// 			if (carriers[i].name == carrier) {
// 				if (carriers[i].check == true){
// 					return true;
// 				}
// 			}
// 		return false;
// 	}

	//========================================DEPART SLIDER=========================================================//

	function loadSlider(){
		$(document).find(".ui-slider-handle").removeClass("ui-state-default ui-corner-all");
		$(document).find(".ui-slider-range").attr('class',"ui-slider-range range-slider-active-segment");
		$(document).find("#slider_depart,#slider_duration,#slider_return,#slider_numofstop").removeClass();
		$(document).find("#slider_depart,#slider_duration,#slider_return,#slider_numofstop").addClass("range-slider");
		handle = $('#slider_depart A.ui-slider-handle');
		handle.eq(0).addClass('range-slider-handle-left');
		handle.eq(1).addClass('range-slider-handle-right');
		handle1 = $('#slider_return A.ui-slider-handle');
		handle1.eq(0).addClass('range-slider-handle-left');
		handle1.eq(1).addClass('range-slider-handle-right');
		handle1 = $('#slider_return A.ui-slider-handle');
		handle1.eq(0).addClass('range-slider-handle-left');
		handle1.eq(1).addClass('range-slider-handle-right');
		handle2 = $('#slider_duration A.ui-slider-handle');
		handle2.eq(0).addClass('range-slider-handle-left');
		handle2.eq(1).addClass('range-slider-handle-right');
		handle3 = $('#slider_numofstop A.ui-slider-handle');
		handle3.addClass('range-slider-handle');
		$(document).on('hover mousedown mousemove', '#slider_numofstop', function() {
			var obj = $(this).find(".ui-slider-handle");
			obj.removeClass("ui-state-hover");
			obj.removeClass("ui-state-focus");
			obj.removeClass("ui-state-active");
			obj.css("outline", "none");
		});
		$(document).on('hover mousedown mousemove', '#slider_depart,#slider_duration,#slider_return', function() {
			var obj = $(this).find(".range-slider-handle-left");
			obj.removeClass("ui-state-hover");
			obj.removeClass("ui-state-focus");
			obj.removeClass("ui-state-active");
			obj.css("outline", "none");	
			var objRight = $(this).find(".range-slider-handle-right");
			objRight.removeClass("ui-state-hover");
			objRight.removeClass("ui-state-focus");
			objRight.removeClass("ui-state-active");
			objRight.css("outline", "none");
		});
}

// 	$(window).load(function(){
//  	 	handle = $('#slider_depart A.ui-slider-handle');
// 		handle.eq(0).addClass('range-slider-handle-left');
// 		handle.eq(1).addClass('range-slider-handle-right');
// 		handle1 = $('#slider_return A.ui-slider-handle');
// 		handle1.eq(0).addClass('range-slider-handle-left');
// 		handle1.eq(1).addClass('range-slider-handle-right');
// 		handle1 = $('#slider_return A.ui-slider-handle');
// 		handle1.eq(0).addClass('range-slider-handle-left');
// 		handle1.eq(1).addClass('range-slider-handle-right');
// 		handle2 = $('#slider_duration A.ui-slider-handle');
// 		handle2.eq(0).addClass('range-slider-handle-left');
// 		handle2.eq(1).addClass('range-slider-handle-right');
// 		handle3 = $('#slider_numofstop A.ui-slider-handle');
// 		handle3.addClass('range-slider-handle');
// 		});

	function convertToTime(val) {
		var mins = (val % 12)*5;
		var hours = Math.floor(val / 12);
		if(hours < 10){
			hours = "0" + hours;
		}
		if(mins<10){
			mins= "0" + mins;
		}
		return hours + "h" + mins;
	}
</script>